package com.example.androidproject.dialog

import android.os.Bundle
import android.provider.CalendarContract.Colors
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.example.androidproject.R

class BottomDialog : DialogFragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {


        return inflater.inflate(R.layout.layout_dialog,container,false)
    }

    override fun onResume() {
        //去除左右边距 和背景 然后需要自己来定义
        //去除左右边距 和背景 然后需要自己来定义
       /* val win = dialog!!.window!!
        dialog!!.window!!.decorView.setPadding(0, 0, 0, 0)
        val lp = win.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        win.attributes = lp
        win.setBackgroundDrawable(null)*/
        super.onResume()
    }
    private fun showAllowingStateLoss(manager: FragmentManager, tag: String?) {
        try {
            val mDismissed = DialogFragment::class.java.getDeclaredField("mDismissed")
            mDismissed.isAccessible = true
            mDismissed.setBoolean(this, false)

            val mShownByMe = DialogFragment::class.java.getDeclaredField("mShownByMe")
            mShownByMe.isAccessible = true
            mShownByMe.setBoolean(this, true)

        } catch (e: Exception) {
            e.printStackTrace()
        }

        val ft = manager.beginTransaction()
        ft.add(this, tag)
        ft.commitAllowingStateLoss()
    }
    fun hintDialog(manger: FragmentManager, isAllowingStateLoss: Boolean = false) {
        try {
            val prev = manger.findFragmentByTag("dialog")
            if (prev != null) {
                if (isAllowingStateLoss) {
                    (prev as DialogFragment).dismissAllowingStateLoss()
                } else {
                    (prev as DialogFragment).dismiss()
                }

            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}
